package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.ClassBean;
import com.project.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("classes")
public class ClassController {
    @Autowired
    private IClassService service;

    @RequestMapping("findAll")
    public String findAll(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, HttpServletRequest request){
        PageInfo<ClassBean>pageInfo=service.findAll(pageNo);
        request.setAttribute("classesObj",pageInfo);
        return  "/html/classes/index.html";
    }

    @RequestMapping("add")
    public String add(ClassBean classBean){
        service.add(classBean);
        return "redirect:/classes/findAll";
    }
    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/classes/findAll";
    }
    @RequestMapping("findById")
    public String findById(int id, Model module){
        ClassBean classObj = service.findById(id);
        module.addAttribute("classObj",classObj);
        return "html/classes/update.html";
    }
    @RequestMapping("update")
    public String update(int id,String classTeacher){
        service.update(id,classTeacher);
        return "redirect:/classes/findAll";
    }
}
